package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniuxy.dto.RecordDetailDTO;
import com.woniuxy.dto.RecordOnAppDTO;
import com.woniuxy.dto.ServiceRecordDTO;
import com.woniuxy.entity.Residents;
import com.woniuxy.entity.ServiceRecord;
import com.woniuxy.mapper.ResidentsMapper;
import com.woniuxy.mapper.ServiceRecordMapper;
import com.woniuxy.service.RecordService;
import com.woniuxy.service.TagsResidentRelationService;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务记录服务实现类
 * 实现服务记录相关的业务逻辑
 */
@Service
@Transactional
public class RecordServiceImpl implements RecordService {
    @Resource
    private ServiceRecordMapper serviceRecordMapper;
    @Resource
    private ResidentsMapper residentsMapper;
    @Resource
    private TagsResidentRelationService relationService;
    
    /**
     * 根据状态查询服务记录
     * @param status 记录状态
     * @return 返回指定状态的记录列表
     */
    @Override
    public ResponseUtil queryRecordByStatus(Integer status,Integer userId,String name) throws ParseException {
        ArrayList<Integer> statusList = new ArrayList<>();
        statusList.add(status);
        List<ServiceRecordDTO> list = serviceRecordMapper.queryAllByServiceStatus(statusList,userId,name);
        return list.size() > 0 ? ResponseUtil.get(ResponseEnum.OK,list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 根据记录ID查询详细信息
     * @param id 记录ID
     * @return 返回指定记录的详细信息
     */
    @Override
    public ResponseUtil queryDetailByRecordId(Integer id) {
        RecordDetailDTO detail = serviceRecordMapper.queryDetailByRecordId(id);
        return detail != null ? ResponseUtil.get(ResponseEnum.OK,detail)
                :ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 更新服务记录
     * @param serviceRecord 服务记录对象
     * @return 返回更新操作的响应结果
     */
    @Override
    public ResponseUtil update(ServiceRecord serviceRecord) {
        Integer result = serviceRecordMapper
                .updateByPrimaryKeySelective(serviceRecord);
        return result > 0 ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 更新记录详情
     * @param detail 记录详情对象
     * @return 返回更新操作的响应结果
     */
    @Override
    public ResponseUtil updateDetail(RecordDetailDTO detail) {
        //分别获取居民和服务的编辑信息，并修改
        Residents resident = new Residents();
        resident.setResidentId(detail.getResidentId());
        resident.setName(detail.getResidentName());
        resident.setIdCard(detail.getIdCard());
        resident.setGender(detail.getGender());
        resident.setDateOfBirth(detail.getBirthday());
        resident.setPhoneNumber(detail.getPhone());
        resident.setCurrentAddress(detail.getAddress());

        List<Integer> ids = detail.getSelectedDiseaseIds();
        Boolean flagTags = relationService
                .updateResidentDiseaseLabels(detail.getResidentId(), ids);

        ServiceRecord serviceRecord = new ServiceRecord();
        serviceRecord.setId(detail.getId());
        serviceRecord.setServiceItem(detail.getServiceItem());
        serviceRecord.setApplyDate(detail.getApplyDate());

        Integer result = residentsMapper.update(resident);
        Integer result1 = serviceRecordMapper.updateByPrimaryKeySelective(serviceRecord);

        return result > 0 && result1 > 0 && flagTags == true
                ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 查询已完成的服务记录
     * @return 返回所有已完成的服务记录
     */
    @Override
    public ResponseUtil queryRecordFinished(Integer userId,String name) throws ParseException {
        ArrayList<Integer> finishedList = new ArrayList<>();
        finishedList.add(3);
        finishedList.add(4);
        List<ServiceRecordDTO> list = serviceRecordMapper.queryAllByServiceStatus(finishedList,userId,name);
        return list.size() > 0 ? ResponseUtil.get(ResponseEnum.OK,list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 添加新的服务记录
     * @param record 服务记录对象
     * @return 返回添加操作的响应结果
     */
    @Override
    public ResponseUtil add(ServiceRecord record) {
        record.setServiceStatus(1);
        record.setIsDeleted(0);
        Integer result = serviceRecordMapper.insert(record);
        return result > 0 ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 根据居民ID查询所有记录（APP端使用）
     * @param residentId 居民ID
     * @return 返回指定居民的所有服务记录
     */
    @Override
    public ResponseUtil queryAllByResidentIdOnApp(Integer residentId) {
        List<RecordOnAppDTO> list = serviceRecordMapper.queryAllByResidentIdOnApp(residentId);
        return list.size() > 0 ? ResponseUtil.get(ResponseEnum.OK,list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findPatientsByDoctorId(Integer doctorId) {
        return null;
    }

    @Override
    public ResponseUtil queryByName(Integer status,String name) {
        ArrayList<Integer> statusList = new ArrayList<>();
        statusList.add(status);
        List<ServiceRecordDTO> list = serviceRecordMapper.queryByName(statusList,name);
        return list.size() > 0 ? ResponseUtil.get(ResponseEnum.OK,list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil queryByNameFinished(Integer userId, String name) {
        ArrayList<Integer> finishedList = new ArrayList<>();
        finishedList.add(3);
        finishedList.add(4);
        List<ServiceRecordDTO> list = serviceRecordMapper.queryByName(finishedList,name);
        return list.size() > 0 ? ResponseUtil.get(ResponseEnum.OK,list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
}
